﻿Imports TyranntUserDatabase
Imports TyranntCharacterDatabase

Public Class CharacterRoutines

    Private cdb As New CharacterDatabaseAccess
    Private udb As New UserDatabaseAccess
    Private _userID As Integer

    Private Const AdminUser As Integer = 1
    Private Const NormalUser As Integer = 2

#Region "Character Routines"

    Public Function DeleteCharacter(ByVal token As String, ByVal characterID As Integer) As String
        GetUserID(token)
        If _userID >= 0 Then
            If cdb.BelongToUser(_userID, characterID) = True Then
                Return cdb.DeleteCharacter(characterID)
            Else
                Return ErrorDataCharacter(-4, "DeleteCharacter")
            End If
        Else
            Return ErrorDataUser(_userID, "GetUserID")
        End If
    End Function

    Public Function NewCharacter(ByVal token As String, ByVal name As String) As String
        GetUserID(token)
        If _userID >= 0 Then
            Return cdb.NewCharacter(_userID, name)
        Else
            Return ErrorDataUser(_userID, "GetUserID")
        End If
    End Function

    Public Function SelectRace(ByVal token As String, ByVal characterID As Integer, ByVal raceID As Integer, ByVal isMale As Boolean, ByVal imageName As String) As String
        GetUserID(token)
        If _userID >= 0 Then
            If cdb.BelongToUser(_userID, characterID) = True Then
                Return cdb.SelectRace(characterID, raceID, isMale, imageName)
            Else
                Return ErrorDataCharacter(-4, "SelectRace")
            End If
        Else
            Return ErrorDataUser(_userID, "GetUserID")
        End If
    End Function

    Public Function SelectGuild(ByVal token As String, ByVal characterID As Integer, ByVal guildID As Integer) As String
        GetUserID(token)
        If _userID >= 0 Then
            If cdb.BelongToUser(_userID, characterID) = True Then
                Return cdb.SelectGuild(characterID, guildID)
            Else
                Return ErrorDataCharacter(-4, "SelectRace")
            End If
        Else
            Return ErrorDataUser(_userID, "GetUserID")
        End If
    End Function

    Public Function GetCharacterSkills(ByVal token As String, ByVal characterID As Integer) As String
        GetUserID(token)
        If _userID >= 0 Then
            If cdb.BelongToUser(_userID, characterID) = True Then
                Return cdb.GetCharacterSkills(characterID)
            Else
                Return ErrorDataCharacter(-4, "GetSkills")
            End If
        Else
            Return ErrorDataUser(_userID, "GetUserID")
        End If
    End Function

    Public Function GetCharacterItems(ByVal token As String, ByVal characterID As Integer) As String
        GetUserID(token)
        If _userID >= 0 Then
            If cdb.BelongToUser(_userID, characterID) = True Then
                Return cdb.GetCharacterItems(characterID)
            Else
                Return ErrorDataCharacter(-4, "GetItems")
            End If
        Else
            Return ErrorDataUser(_userID, "GetUserID")
        End If
    End Function

    Public Function GenerateStats(ByVal token As String, ByVal characterID As Integer) As String
        GetUserID(token)
        If _userID >= 0 Then
            If cdb.BelongToUser(_userID, characterID) = True Then
                Return cdb.GenerateStats(characterID)
            Else
                Return ErrorDataCharacter(-4, "GenerateStats")
            End If
        Else
            Return ErrorDataUser(_userID, "GetUserID")
        End If
    End Function

    Public Function GetCharacter(ByVal token As String, ByVal characterID As Integer) As String
        GetUserID(token)
        If _userID >= 0 Then
            If cdb.BelongToUser(_userID, characterID) = True Then
                Return cdb.GetPlayerCharacter(characterID)
            Else
                Return ErrorDataCharacter(-4, "GetCharacter")
            End If
        Else
            Return ErrorDataUser(_userID, "GetUserID")
        End If
    End Function

    Public Function GetCharacters(ByVal token As String) As String
        GetUserID(token)
        If _userID >= 0 Then
            Return cdb.GetCharacters(_userID)
        Else
            Return ErrorDataUser(_userID, "GetUserID")
        End If
    End Function
#End Region

#Region "Skill Routines"

    Public Function AddSkill(ByVal token As String, ByVal name As String, ByVal description As String) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataUser(_userID, "AddSkill")
        End If
        If IsAdmin() Then
            Return cdb.AddSkill(name, description)
        Else
            Return ErrorDataCharacter(-3, "AddSkill")
        End If

    End Function

    Public Function UpdateSkill(ByVal token As String, ByVal skillID As Integer, ByVal name As String, ByVal description As String) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataUser(_userID, "UpdateSkill")
        End If
        If IsAdmin() Then
            Return cdb.UpdateSkill(skillID, name, description)
        Else
            Return ErrorDataCharacter(-3, "UpdateSkill")
        End If
    End Function

    Public Function DeleteSkill(ByVal token As String, ByVal skillID As Integer) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataUser(_userID, "DeleteSkill")
        End If
        If IsAdmin() Then
            Return cdb.DeleteSkill(skillID)
        Else
            Return ErrorDataCharacter(-3, "DeleteSkill")
        End If
    End Function

    Public Function GetSkills(ByVal token As String) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataUser(_userID, "GetSkills")
        End If
        Return cdb.GetSkills()
    End Function

#End Region

#Region "Race Routines"

    Public Function AddRace(ByVal token As String, ByVal raceXML As String) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataUser(_userID, "AddRace")
        End If
        If IsAdmin() Then
            Return cdb.AddRace(raceXML)
        Else
            Return ErrorDataCharacter(-3, "AddRace")
        End If

    End Function

    Public Function UpdateRace(ByVal token As String, ByVal raceXML As String) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataUser(_userID, "UpdateRace")
        End If
        If IsAdmin() Then
            Return cdb.UpdateRace(raceXML)
        Else
            Return ErrorDataCharacter(-3, "UpdateRace")
        End If
    End Function

    Public Function DeleteRace(ByVal token As String, ByVal raceID As Integer) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataUser(_userID, "DeleteRace")
        End If
        If IsAdmin() Then
            Return cdb.DeleteRace(raceID)
        Else
            Return ErrorDataCharacter(-3, "DeleteRace")
        End If
    End Function

    Public Function GetRaces(ByVal token As String) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataUser(_userID, "GetRaces")
        End If
        Return cdb.GetRaces()
    End Function

    Public Function GetRace(ByVal token As String, ByVal raceID As Integer) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataUser(_userID, "GetRace")
        End If
        Return cdb.GetRace(raceID)
    End Function

#End Region

#Region "Item Routines"

    Public Function AddItem(ByVal token As String, ByVal itemXML As String) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataUser(_userID, "AddItem")
        End If
        If IsAdmin() Then
            Return cdb.AddItem(itemXML)
        Else
            Return ErrorDataCharacter(-3, "AddItem")
        End If

    End Function

    Public Function UpdateItem(ByVal token As String, ByVal itemXML As String) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataUser(_userID, "UpdateItem")
        End If
        If IsAdmin() Then
            Return cdb.UpdateItem(itemXML)
        Else
            Return ErrorDataCharacter(-3, "UpdateItem")
        End If
    End Function

    Public Function DeleteItem(ByVal token As String, ByVal itemID As Integer) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataUser(_userID, "DeleteItem")
        End If
        If IsAdmin() Then
            Return cdb.DeleteItem(itemID)
        Else
            Return ErrorDataCharacter(-3, "DeleteItem")
        End If
    End Function

    Public Function GetItems(ByVal token As String) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataUser(_userID, "GetItems")
        End If
        Return cdb.GetItems()
    End Function

    Public Function GetItem(ByVal token As String, ByVal itemID As Integer) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataUser(_userID, "GetItem")
        End If
        Return cdb.GetItem(itemID)
    End Function

    Public Function GetItemTypes(ByVal token As String) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataUser(_userID, "GetItems")
        End If
        Return cdb.GetItemTypes()
    End Function

#End Region

#Region "Class Routines"

    Public Function AddClass(ByVal token As String, ByVal classXML As String) As String
        GetUserID(token)
        If IsAdmin() = False Then
            Return ErrorDataCharacter(-3, "AddClass")
        End If
        Return cdb.AddClass(classXML)
    End Function

    Public Function UpdateClass(ByVal token As String, ByVal classXML As String) As String
        GetUserID(token)
        If IsAdmin() = False Then
            Return ErrorDataCharacter(-3, "UpdateClass")
        End If
        Return cdb.UpdateClass(classXML)
    End Function

    Public Function DeleteClass(ByVal token As String, ByVal classID As Integer) As String
        GetUserID(token)
        If IsAdmin() = False Then
            Return ErrorDataCharacter(-3, "DeleteClass")
        End If
        Return cdb.DeleteClass(classID)
    End Function

    Public Function GetClasses(ByVal token As String) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataCharacter(_userID, "GetClasses")
        End If
        Return cdb.GetClasses()
    End Function

    Public Function GetClass(ByVal token As String, ByVal classID As Integer) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataCharacter(_userID, "GetClass")
        End If
        Return cdb.GetClass(classID)
    End Function
#End Region

#Region "Guild Routines"

    Public Function AddGuild(ByVal token As String, ByVal guildXML As String) As String
        GetUserID(token)
        If IsAdmin() = False Then
            Return ErrorDataCharacter(-3, "AddGuild")
        End If
        Return cdb.AddGuild(guildXML)
    End Function

    Public Function UpdateGuild(ByVal token As String, ByVal guildXML As String) As String
        GetUserID(token)
        If IsAdmin() = False Then
            Return ErrorDataCharacter(-3, "UpdateGuild")
        End If
        Return cdb.UpdateGuild(guildXML)
    End Function

    Public Function DeleteGuild(ByVal token As String, ByVal guildID As Integer) As String
        GetUserID(token)
        If IsAdmin() = False Then
            Return ErrorDataCharacter(-3, "DeleteGuild")
        End If
        Return cdb.DeleteGuild(guildID)
    End Function

    Public Function GetGuilds(ByVal token As String) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataCharacter(_userID, "GetUserID")
        End If
        Return cdb.GetGuilds()
    End Function

    Public Function GetGuild(ByVal token As String, ByVal guildID As Integer) As String
        GetUserID(token)
        If _userID < 0 Then
            Return ErrorDataCharacter(_userID, "GetUserID")
        End If
        Return cdb.Getguild(guildID)
    End Function

#End Region

#Region "General Routines"

    Private Function IsAdmin() As Boolean
        Dim userGroup As Integer = udb.GetUserGroup(_userID)
        If userGroup = AdminUser Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub GetUserID(ByVal token As String)
        _userID = udb.GetUserIDFromToken(token)
    End Sub

    Private Function ErrorDataUser(ByVal code As Integer, ByVal routineName As String) As String
        Select Case code
            Case -1
                Return "[#]usernotexist[" + routineName + "]"
            Case -2
                Return "[#]failed[" + routineName + "] - " + udb.LastError.ToString
            Case Else
                Return "[#]unknowncode[" + code.ToString + "],[" + routineName + "]"
        End Select
    End Function

    Private Function ErrorDataCharacter(ByVal code As Integer, ByVal routineName As String) As String
        Select Case code
            Case -1
                Return "[#]usernotexist[" + routineName + "]"
            Case -2
                Return "[#]failed[" + routineName + "] - " + cdb.LastError.ToString
            Case -3
                Return "[#]permissiondenied[" + routineName + "]"
            Case -4
                Return "[#]characternotbelongtoyou[" + routineName + "]"
            Case Else
                Return "[#]unknowncode[" + code.ToString + "],[" + routineName + "]"
        End Select
    End Function

#End Region

End Class
